﻿using Business;
using MB.Orm.DB;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Public;

namespace MvcAppWeb.Controllers
{
    public class BaseDataController : Controller
    {
        //
        // GET: /BaseData/

        //public ActionResult Index()
        //{
        //    return View();
        //}

        /// <summary>
        /// 供应商列表
        /// </summary>
        /// <returns></returns>
        public JsonResult VenderList(int VenderID = 0)
        {
            List<Vender> retData = new List<Vender>();
            NewCommon nec = new NewCommon();
            retData = nec.GetVenderList();
            if (VenderID != 0)
            {
                retData = retData.Where(_ => _.VENDERID == VenderID).ToList();
            }
            return Json(retData);
        }

        public JsonResult GoodsCodeList(int IsSetAll, int VenderID)
        {
            List<Goods> retData = new List<Goods>();
            NewCommon nec = new NewCommon();
            retData = nec.GetGoodsCodeList(IsSetAll, VenderID);
            return Json(retData);
        }

        public bool CheckLicense(VenderUser _VenderUser)
        {
            bool result = true;
            DateTime TimeNow = DateTime.Now;
            List<VenderUser_License> lstVenderUser_License = new List<VenderUser_License>();
            lstVenderUser_License = new BVenderUserLicense().CheckVenderUserLicense(_VenderUser.VENDERID);
            var retData = new object();
            retData = new SysEnum().GetEnumList(typeof(SysEnum.LicenseType), false, false);
            if (lstVenderUser_License != null && lstVenderUser_License.Count > 0)
            {
                result = result && lstVenderUser_License.Where(_ => _.LICENSE_TYPE == 1 && _.VALIDDATE.Date >= TimeNow.Date).Count() > 0;
                result = result && lstVenderUser_License.Where(_ => _.LICENSE_TYPE == 2 && _.VALIDDATE.Date >= TimeNow.Date).Count() > 0;
                result = result && lstVenderUser_License.Where(_ => _.LICENSE_TYPE == 3 && _.VALIDDATE.Date >= TimeNow.Date).Count() > 0;
                result = result && (lstVenderUser_License.Where(_ => _.LICENSE_TYPE == 4 && _.VALIDDATE.Date >= TimeNow.Date).Count() > 0
                    || lstVenderUser_License.Where(_ => _.LICENSE_TYPE == 5 && _.VALIDDATE.Date >= TimeNow.Date).Count() > 0);
            }
            else
            {
                result = false;
            }
            return result;
        }

        public string ImportFile()
        {
            string sjson = string.Empty;

            HttpPostedFileBase file = Request.Files["files"];
            string FileName;
            string savePath;
            if (file == null || file.ContentLength <= 0)
            {
                sjson = "{success:false,msg:'文件不能为空'}";
                return sjson;
            }
            string filename = Path.GetFileName(file.FileName);
            int filesize = file.ContentLength;//获取上传文件的大小单位为字节byte
            string fileEx = Path.GetExtension(filename);//获取上传文件的扩展名
            string NoFileName = Path.GetFileNameWithoutExtension(filename);//获取无扩展名的文件名
            int Maxsize = 4000 * 1024;//定义上传文件的最大空间大小为4M
            string FileType = ".jpg,.bmp,.tiff,.gif,.png";//定义上传文件的类型字符串

            FileName = NoFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx;
            if (!FileType.Contains(fileEx))
            {
                sjson = "{success:false,msg:'文件类型不对，只能导入bmp,jpg,tiff,gif,.png格式的文件'}";
                return sjson;
            }
            string imgpath = "upload/license/" + DateTime.Now.ToString("yyyyMM");
            string path = AppDomain.CurrentDomain.BaseDirectory + imgpath;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (filesize >= Maxsize)
            {
                sjson = "{success:false,msg:'上传文件超过4M，不能上传'}";
                return sjson;
            }
            savePath = Path.Combine(path, FileName);
            file.SaveAs(savePath);
            sjson = "{success:true,msg:'" + imgpath + "/" + FileName + "',ImgName:'" + filename + "'}";
            return sjson;
        }
    }
}
